<?php

require ('config.php');

// filtering sql query for easy troubleshooting
function safe_query($query = "") {
	if(empty($query)){
		return false;
	}
	$result = mysql_query($query);
	if ($result) {
	  return $result;
	} else {
	  $log = fopen("failquery.log", "ab");
	  $recDate = date("d-m-Y H:i");
	  $loginfo = "[$recDate] query failed: errorno=".mysql_errno().";error=".mysql_error().";\nquery=".$query."\n";
	  fwrite($log, $loginfo);
	  fclose($log);
	  return FALSE;
	}
}

// query untuk cek login
function getLogin($username, $password) {
	$query = "SELECT *, pegawai.nama_pegawai as `nama` FROM `user` JOIN `pegawai` ON pegawai.nip = user.nip 
			  WHERE `username` = '".$username."' AND `password` = '".$password."'";
	$result = safe_query($query);
	if(($result) && (mysql_num_rows($result) > 0)) {
		$data = array();
		while($row = mysql_fetch_assoc($result))
		$data[] = $row;
		return $data;
	} else {
		return FALSE;
	}
}

